import { Book } from '@libs/db/models/book.model';
import { Category } from '@libs/db/models/category.model';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ReturnModelType } from '@typegoose/typegoose';
import { InjectModel } from 'nestjs-typegoose';
import { BooksService } from './books.service';

@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('图书')
@Controller('books')
export class BooksController {
  constructor(
    private readonly booksService: BooksService,
    @InjectModel(Category)
    private readonly categoryModel: ReturnModelType<typeof Category>,
  ) {}

  @ApiOperation({ summary: '创建图书' })
  @Post()
  create(@Body() book: Book) {
    return this.booksService.create(book);
  }

  @ApiOperation({ summary: '图书列表' })
  @Get()
  findAll(@Query() query: any) {
    return this.booksService.findAll(query);
  }

  @ApiOperation({ summary: '查询图书' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.booksService.findOne(id);
  }

  @ApiOperation({ summary: '修改图书' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() book: Book) {
    return this.booksService.update(id, book);
  }

  @ApiOperation({ summary: '删除图书' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.booksService.remove(id);
  }

  @ApiOperation({ summary: '图书配置' })
  @Post('options')
  async bookConfigs(): Promise<any> {
    const categories = await this.categoryModel.find();
    return {
      title: '图书列表',
      descriptions: '显示和处理图书列表',
      items: [
        {
          label: '图书名称',
          prop: 'name',
          type: 1,
        },
        {
          label: '作者',
          prop: 'author',
          type: 1,
        },
        {
          label: '图书分类',
          prop: 'categories',
          keyName: 'name',
          type: 3,
          items: categories,
        },
        {
          label: '图书单价',
          prop: 'price',
          type: 1,
        },
        {
          label: '库存',
          prop: 'number',
          type: 1,
        },
        {
          label: '图书封面',
          prop: 'image',
          type: 4,
        },
        {
          label: '图书简介',
          prop: 'description',
          type: 1,
        },
      ],
    };
  }
}
